package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.web.ProjectedPayload;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-02
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        deleteListAll();
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        deleteListAll();

        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        deleteListAll();

        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        deleteListAll();

        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理项目
     *
     * @return 护理项目列表
     */
    @Override
    public List<NursingProjectVo> getAll() {
        //先判断缓存中是否存在
        List<NursingProjectVo>list=(List<NursingProjectVo>)redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_ALL_KEY);

        if(ObjectUtil.isNotEmpty(list))
        {
            return list;
        }
        list = nursingProjectMapper.getAll();
        return list;
    }

    //清除缓存
    private void deleteListAll() {
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
    }



    //小程序端获取护理项目
    public TableDataInfo<NursingProject> PageByNameAndStatus(String name, Integer pageNum, Integer pageSize, Integer status) {

        Page<NursingProject> page = Page.of(pageNum, pageSize);

        //设置条件
        page=page(page,Wrappers.<NursingProject>lambdaQuery()
                .like(name!= null&& !"".equals(name),NursingProject::getName,name)
                .eq(status!= null&& !"".equals(status),NursingProject::getStatus,status));

        TableDataInfo<NursingProject> tableDataInfo=new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());;
        tableDataInfo.setRows(page.getRecords());

        return tableDataInfo;
    }
}
